package com.grape.api;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageInfo;
import com.grape.annotation.OperateMethodName;
import com.grape.controller.BaseController;
import com.grape.result.R;
import com.grape.vo.BaseVo;
import com.grape.vo.RoleMenuVO;
import com.grape.vo.RolePermissionVO;
import com.grape.entity.SysRole;
import com.grape.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;


/**
 * 角色信息表 控制器
 * @author  putao
 * @email   2628279194@qq.com
 */
@RestController
@RequestMapping("roles")
@Api(tags = "角色接口")
@AllArgsConstructor
public class SysRoleController extends BaseController<SysRole> {

    private final ISysRoleService sysRoleService;

    @GetMapping
    @ApiOperation(value = "角色列表")
    @OperateMethodName(value = "角色信息表列表", url = "system:roles:listPage")
    public R<?> listPage(BaseVo baseVo) {
        PageInfo<SysRole> pageInfo = sysRoleService.pageListByDelFlag(baseVo);
        return CollectionUtil.isNotEmpty(pageInfo.getList()) ? selectSuccess(pageInfo, baseVo) : selectNotFound(pageInfo, baseVo);
    }

    @PutMapping
    @ApiOperation(value = "角色修改")
    @OperateMethodName(value = "角色信息表修改", url = "system:roles:update")
    public R<?> update(@Validated @RequestBody SysRole sysRole){
        if (!Strings.isNotBlank(sysRole.getId())){
            return updateNotFound(sysRole, null);
        }
        sysRoleService.updateData(sysRole);
        return updateSuccess(null, null);
    }


    @PostMapping
    @ApiOperation(value = "添加角色")
    @OperateMethodName(value = "添加角色信息表", url = "system:roles:insert")
    public R<?> insert(@Validated @RequestBody SysRole sysRole){
        return insertSuccess(sysRoleService.insertData(sysRole), null);
    }

    @DeleteMapping
    @ApiOperation(value = "删除角色，可批量多个id逗号拼接")
    @OperateMethodName(value = "角色信息表删除,批量", url = "system:roles:delete")
    public R<?> delete(@NotBlank(message = "删除时ids不能为空")  @RequestParam String ids){
        int number = sysRoleService.deleteData(ids);
        if (number < 0) {
            return deleteNotFound(ids, number);
        }
        return deleteSuccess(ids, null);
    }

    @GetMapping("detail")
    @ApiOperation(value = "角色详情")
    @OperateMethodName(value = "角色信息表详情", url = "system:roles:detail")
    public R<?> detail(@NotBlank(message = "详情时id不能为空") String id){
        SysRole sysRole = sysRoleService.detail(id);
        if (sysRole != null) {
            return selectSuccess(sysRole, id);
        }
        return selectNotFound(id, null);
    }

    @PostMapping("updateRolePermissionList")
    @ApiOperation(value = "修改角色的权限列表")
    @OperateMethodName(value = "修改角色的权限列表", url = "system:roles:updateRolePermissionList")
    public R<?> updateRolePermissionList(@RequestBody @Validated RolePermissionVO rolePermissionVo){
        sysRoleService.updateRolePermissionList(rolePermissionVo);
        return operationSuccess(rolePermissionVo, null);
    }

    @PostMapping("updateRoleMenuList")
    @ApiOperation(value = "修改角色的菜单列表")
    @OperateMethodName(value = "修改角色的菜单列表", url = "system:roles:updateRoleMenuList")
    public R<?> updateRoleMenuList(@RequestBody @Validated RoleMenuVO roleMenuVo){
        sysRoleService.updateRoleMenuList(roleMenuVo);
        return operationSuccess(roleMenuVo, null);
    }

    @GetMapping("listAll")
    @ApiOperation(value = "角色全部列表")
    @OperateMethodName(value = "角色全部列表", url = "system:roles:listAll")
    public R<?> listAll() {
        List<SysRole> roleList = sysRoleService.listAll();
        return CollectionUtil.isNotEmpty(roleList) ? selectSuccess(roleList, null) : selectNotFound(roleList, null);
    }
}

